//
//  kata_CommentToolbar.m
//  CityStar
//
//  Created by mac on 12-9-3.
//  Copyright (c) 2012年 kata. All rights reserved.
//

#import "kata_CommentToolbar.h"

#import <QuartzCore/QuartzCore.h>
#import "HTDefine.h"

@interface kata_CommentToolbar (/* private method */)
{
    UIButton * mask;
    
    CGFloat * yTool;
}

@property (nonatomic, retain) UITextView * commentTf;

@property (nonatomic, retain) UIButton * postBtn;

- (void)post;
- (UIBarButtonItem *)createSpaceItem;

- (void)keyboardWillShow:(NSNotification *)notif;
- (void)keyboardWillHide:(NSNotification *)notif;

- (void)showMask;
- (void)hideMask;


@end

@implementation kata_CommentToolbar

@synthesize commentToolbarDelegate = __commentToolbarDelegate;

@synthesize commentTf = __commentTf;

@synthesize postBtn = __postBtn;

- (void)dealloc
{
    self.commentTf = nil;
    self.postBtn = nil;
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    if (mask) {
        [mask removeFromSuperview];
        mask = nil;
    }
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
//        self.tintColor = rgba(185, 191, 198, 1);
        
        NSMutableArray * itemsArr = [NSMutableArray arrayWithCapacity:5];
        
//        self.backgroundColor = rgba(254, 241, 219, 1);
//        self.translucent = NO;
        if (iOS7) {
//            self.barTintColor = rgba(255, 243, 226, 1);
        }
        if ([[[UIDevice currentDevice]systemVersion]floatValue] >=8.0) {
            [itemsArr addObject:[self createSpaceItem]];
        }
        
        __commentTf = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth-80, 30)];
//        __commentTf.borderStyle = UITextBorderStyleRoundedRect;
        __commentTf.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        __commentTf.font = [UIFont systemFontOfSize:13];
        __commentTf.layer.cornerRadius = 5;
        __commentTf.layer.borderColor = [rgba(204, 204, 204, 1) CGColor];
        __commentTf.layer.borderWidth = 1;
        __commentTf.layer.masksToBounds = YES;
        
        
        __commentTf.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
        
        __commentTf.delegate = self;
        
        __commentTf.returnKeyType = UIReturnKeyDefault;
        
        UIBarButtonItem * inputItem = [[UIBarButtonItem alloc] initWithCustomView:__commentTf];
        
        [itemsArr addObject:inputItem];
        
//        [itemsArr addObject:[self createSpaceItem]];
        
        UIButton * postBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 28)];
        
        postBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
        
//        UIImage * normalBg = [[UIImage imageNamed:@"commentBtn.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:5];
//        UIImage * selectedBg = [[UIImage imageNamed:@"commentBtn_selected.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:5];
        
        [postBtn setBackgroundImage:[UIImage imageNamed:@"msg_btn_send_selected"] forState:UIControlStateNormal];
//        [postBtn setBackgroundImage:selectedBg forState:UIControlStateHighlighted];
//        [postBtn setBackgroundImage:selectedBg forState:UIControlStateSelected];
        postBtn.backgroundColor = rgba(251, 183, 92, 1);
        postBtn.layer.cornerRadius = 4.0;
        
//        [postBtn setTitleColor:rgba(255, 255, 255, 1) forState:UIControlStateNormal];
//        
//        [[postBtn titleLabel] setFont:[UIFont boldSystemFontOfSize:14]];
//        [[postBtn titleLabel] setShadowColor:rgba(120, 120, 120, 1)];
//        [[postBtn titleLabel] setShadowOffset:CGSizeMake(0, -1)];
//        
        [postBtn setTitle:@"发送" forState:UIControlStateNormal];
        
        [postBtn addTarget:self action:@selector(post) forControlEvents:UIControlEventTouchUpInside];
        
        self.postBtn = postBtn;
        
        UIBarButtonItem * postBtnItem = [[UIBarButtonItem alloc] initWithCustomView:postBtn];
        
        [itemsArr addObject:postBtnItem];
        
//        [itemsArr addObject:[self createSpaceItem]];
        
        self.items = itemsArr;
        
        
//        添加键盘高度监控
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
        
//        if (![KATACheckUtils isOS4]) {
//            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];
//        }
        
    }
    return self;
}

- (UIBarButtonItem *)createSpaceItem
{
    
    UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spaceItem.width = 10;
    
    return spaceItem;
}

- (void)post
{
    if (self.commentToolbarDelegate && [self.commentToolbarDelegate respondsToSelector:@selector(postCommentData:toolbar:)]) {
        [self.commentToolbarDelegate postCommentData:self.commentTf.text toolbar:self];
    }
}

#pragma mark - 键盘事件
- (void)keyboardWillHide:(NSNotification *)notif
{
    NSDictionary * userInfo = [notif userInfo];
    
    if (![self.commentTf isFirstResponder]) {
        return;
    }
    
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    
    NSTimeInterval animationDuration;
    
    [animationDurationValue getValue:&animationDuration];
    
    NSValue * animationCurveValue = [userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    
    UIViewAnimationCurve animationCurve;
    [animationCurveValue getValue:&animationCurve];
    
    if (self.commentToolbarDelegate && [self.commentToolbarDelegate respondsToSelector:@selector(reposWithKeyboardHeight:animationDuration:curve:)]) {
        
        [self.commentToolbarDelegate reposWithKeyboardHeight:0.0f animationDuration:animationDuration curve:animationCurve];
    }
    
}
- (void)keyboardWillShow:(NSNotification *)notif
{
    NSDictionary * userInfo = [notif userInfo];
    
    
    if (![self.commentTf isFirstResponder]) {
        return;
    }
    
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    
    NSTimeInterval animationDuration;
    
    [animationDurationValue getValue:&animationDuration];
    
    NSValue * animationCurveValue = [userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    
    UIViewAnimationCurve animationCurve;
    [animationCurveValue getValue:&animationCurve];
    
    if (self.commentToolbarDelegate && [self.commentToolbarDelegate respondsToSelector:@selector(reposWithKeyboardHeight:animationDuration:curve:)]) {
        
        
        [self.commentToolbarDelegate reposWithKeyboardHeight:CGRectGetHeight(keyboardRect) animationDuration:animationDuration curve:animationCurve];
    }
    
}
#pragma textfield 

- (void)startEdit
{
    [self.commentTf becomeFirstResponder];
}

- (void)showMask
{
    if (!mask) {
        mask = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.superview.frame))];
        mask.backgroundColor = rgba(0, 0, 0, 0);
        [mask addTarget:self action:@selector(didStopEdit) forControlEvents:UIControlEventTouchUpInside];
        
        [self.superview insertSubview:mask belowSubview:self];
        
    }
}

- (void)didStopEdit
{
    [self.commentTf resignFirstResponder];
    
    [self hideMask];
    
    self.commentTf.text = nil;
}

- (void)hideMask
{
    [mask removeFromSuperview];
    mask = nil;
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    [self showMask];
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSString *strValue = [textView.text stringByReplacingCharactersInRange:range withString:text];
    
    CGSize contentSize = [strValue sizeWithFont:self.commentTf.font constrainedToSize:CGSizeMake(self.commentTf.frame.size.width, 10000) lineBreakMode:NSLineBreakByWordWrapping];
    CGFloat height = (contentSize.height+30) > 49 ? (contentSize.height+30) : 49;
    height = height > 79 ? 79:height;
    self.frame = CGRectMake(0, CGRectGetMaxY(self.frame)-height, CGRectGetWidth(self.frame), height);
    
    return YES;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
